package org.pku_uestc.hackathon;

import net.tsz.afinal.FinalActivity;
import net.tsz.afinal.annotation.view.ViewInject;

import org.pku_uestc.hackathon.bean.Sensor;
import org.pku_uestc.hackathon.sensor.SmartLight;
import org.pku_uestc.hackathon.sensor.SmartLight.SmartLightHandler;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;

public class SmartLightActivity extends FinalActivity {
	// 无需调用findViewById和setOnclickListener等
	@ViewInject(id = R.id.tv)
	TextView textView;
	@ViewInject(id = R.id.bt, click = "btnClick")
	Button button;
	SmartLight sl;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		final SeekBar bsb = (SeekBar) findViewById(R.id.BrighnessSeekBar);
		final SeekBar hsb = (SeekBar) findViewById(R.id.HueSeekBar);
		textView.setText("当前进度：" + bsb.getProgress());
		sl = SmartLight.getInstantces(58);
		// 设置拖动条改变监听器
		OnSeekBarChangeListener osbcl = new OnSeekBarChangeListener() {

			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
			}

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
			}

			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				Toast.makeText(getApplicationContext(), "onStopTrackingTouch",
						Toast.LENGTH_SHORT).show();
				Float brightness = ((float) (bsb.getProgress()) / 100);
				Toast.makeText(getApplicationContext(), brightness + "",
						Toast.LENGTH_SHORT).show();
				sl.changeState(null, true, brightness + "", null,
						new SmartLightHandler() {

							@Override
							protected void successRefresh() {
								// TODO Auto-generated method stub
								textView.setText("succ" + Sensor.token);
							}

							@Override
							protected void errorRefresh() {
								// TODO Auto-generated method stub
								textView.setText("false" + Sensor.token);
							}
						});
			}

		};

		OnSeekBarChangeListener osbcl2 = new OnSeekBarChangeListener() {

			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				Toast.makeText(getApplicationContext(), "onStopTrackingTouch",
						Toast.LENGTH_SHORT).show();
				Float hue = ((float) (hsb.getProgress()) / 100);
				Toast.makeText(getApplicationContext(), hue + "",
						Toast.LENGTH_SHORT).show();
				sl.changeState(null, true, null, hue + "",
						new SmartLightHandler() {

							@Override
							protected void successRefresh() {
								// TODO Auto-generated method stub
								textView.setText("succ" + Sensor.token);
							}

							@Override
							protected void errorRefresh() {
								// TODO Auto-generated method stub
								textView.setText("false" + Sensor.token);
							}
						});
			}

			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub

			}

		};
		// 为拖动条绑定监听器
		bsb.setOnSeekBarChangeListener(osbcl);
		hsb.setOnSeekBarChangeListener(osbcl2);
	}

	public void btnClick(View v) {
		sl.changeState(null, true, null, null, new SmartLightHandler() {

			@Override
			protected void successRefresh() {
				// TODO Auto-generated method stub
				textView.setText("succ" + Sensor.token);
			}

			@Override
			protected void errorRefresh() {
				// TODO Auto-generated method stub
				textView.setText("false" + Sensor.token);
			}
		});
	}
}